home *** CD-ROM | disk | FTP | other *** search
- Path: nntp.coast.net!torn!nott!cunews!usenet
- From: tcope@chat.carleton.ca (Tyler Cope)
- Newsgroups: comp.lang.c
- Subject: memory allocation using malloc and free
- Date: Mon, 19 Feb 1996 17:36:47 GMT
- Organization: Carleton University
- Message-ID: <4gagll$5rc@bertrand.ccs.carleton.ca>
- Reply-To: tcope@chat.carleton.ca
- NNTP-Posting-Host: alpha02.scs.carleton.ca
- X-Newsreader: Forte Free Agent 1.0.82
-
- Could someone please demonstrate the proper (or _a_ proper) ANSI
- method of allocating >64K space using malloc and free. I've tried many
- versions and most work when malloc() and free() are both called from
- main(), but when a separate function is called, everything goes crazy.
-
- To me, I don't see any reason why the following does not work, but it
- doesn't... it hangs up on me.
-
- #include <process.h>
- #include <stdio.h>
- #include <alloc.h>
-
- int main(void)
- {
- void get_mem(unsigned char far *);
- void free_mem(unsigned char far *);
-
- unsigned char far *ptr;
-
- get_mem(ptr);
- freemem(ptr);
-
- return(1);
- }
-
- void get_mem(unsigned char far *fptr)
- {
- if ((fptr = (unsigned char far *)malloc(320*200+1)) == NULL)
- {
- printf("Could not allocate enough memory");
- exit(1);
- }
- }
-
- void freemem(unsigned char far *fptr)
- {
- /* I think the problem may be here.
- I've tried farfree((void far *)fptr); here as well, which
- is availible to me because I'm using Borland Turbo C++ but i'd
- like
- to be as portable as possible. free() takes a near pointer right? so
- how do you free a far pointer using a function that requires a near
- pointer? */
- free((void far *)fptr);
- }
-
- tcope@chat.carleton.ca
-
-
-